package com.igame.maingame;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.TreeMap;

import com.jme.bounding.BoundingBox;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.InputActionInterface;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.util.Timer;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsNode;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.contact.ContactCallback;
import com.jmex.physics.contact.ContactInfo;
import com.jmex.physics.contact.PendingContact;
import com.igame.maingame.*;

public class BounusItemManager {
	public class BounusItem extends Box{
		private float mark = 10;
		public float getMark(){
			return mark;
		}
	    public BounusItem(String name, Vector3f center, float x, float y, float z) {
	    	super(name,  center,  x,  y,  z);
	    }
		
	}
	
	private Map<String, DynamicPhysicsNode> itemlist = new TreeMap<String, DynamicPhysicsNode>();
	private PhysicsSpace physpace;
	private Node scene;
	private Node allparent;
	int itemid = 0;
	private PhysicsSpace getPhysicsSpace(){
		return physpace;
	}
	public BounusItemManager( Node sceneroot, PhysicsSpace physicsSpace){
		scene = sceneroot;
		physpace = physicsSpace;
		allparent = new Node("itemballroot");
		scene.attachChild(allparent);
		updateScene();
	}
	
	private void updateScene(){
		scene.updateGeometricState(0, true);
		scene.updateRenderState();
	}
	
	float def_box_x = 1;
	float def_box_y = 1;
	float def_box_z = 1;
	Vector3f def_box_scale = Vector3f.UNIT_XYZ.mult(0.8f);
	public void addItem( Vector3f pos ){
		BounusItem item = new BounusItem(String.format("bounus_%d", itemid++),Vector3f.ZERO,def_box_x,def_box_y,def_box_z);
		item.xExtent = def_box_x;
		item.yExtent = def_box_y;
		item.zExtent = def_box_z;
		item.setModelBound(new BoundingBox());
		item.updateModelBound();
		item.setLocalScale(def_box_scale);
		DynamicPhysicsNode node = getPhysicsSpace().createDynamicNode();
		node.attachChild(item);
		node.generatePhysicsGeometry();
		node.setLocalTranslation(pos.clone());
		node.setName(item.getName());
		node.setUserData(OBJECT_TYPE.class.getName(), OBJECT_TYPE.BOUNUS_ITEM);
		itemlist.put(node.getName(), node);
		
		allparent.attachChild(node);
		updateScene();
	}
	
	public void clear(){
		for(DynamicPhysicsNode node:itemlist.values()){
			node.delete();
		}
		updateScene();
		itemlist.clear();
	}
	
	public void destroyItem(String name){
		DynamicPhysicsNode node= itemlist.get(name);
		itemlist.remove(name);
		node.delete();
		updateScene();
	}
	
	public class ItemPickedContactCallback implements ContactCallback{
		@Override
		public boolean adjustContact(PendingContact contact) {
			PhysicsNode node1 = contact.getNode1();
			PhysicsNode node2 = contact.getNode2();
			boolean done = false;
			
//			Object t1 = node1.getUserData(OBJECT_TYPE.class.getName());
//			Object t2 = node1.getUserData(OBJECT_TYPE.class.getName());
//			if((t1!=null && ((OBJECT_TYPE)t1)==OBJECT_TYPE.BOUNUS_ITEM)
//					|| (t2!=null && ((OBJECT_TYPE)t2)==OBJECT_TYPE.BOUNUS_ITEM)){
//				done = false;
//			}else{
//				return false;
//			}
			
			if(!(node1.getChild(0) instanceof BounusItem) 
					&& !(node2.getChild(0) instanceof BounusItem)){
				return false;
			}
			
			//check for control ball
			ControlBall cb1 = ControlBall.getControlBall(node1);
			ControlBall cb2 = ControlBall.getControlBall(node2);
			ControlBall cbtarget = null;
			PhysicsNode ballnode = null;
			PhysicsNode itemnode = null;
			done = true;
			if(cb1 != null){
				ballnode = node1;
				itemnode = node2;
				cbtarget = cb1;
			}else if(cb2 != null){
				ballnode = node2;
				itemnode = node1;
				cbtarget = cb2;
			}else{
				done = false;
			}
			if(done){
				cbtarget.addMark(10);
				contact.setIgnored(true);
				destroyItem(itemnode.getName());
				System.out.println(cbtarget.getMark());
				return true;
			}
			
			//check for soldier ball
			SoldierBallManager.SoldierBall sb1 = SoldierBallManager.getSoldierBallFromNode(node1);
			SoldierBallManager.SoldierBall sb2 = SoldierBallManager.getSoldierBallFromNode(node2);
			SoldierBallManager.SoldierBall sbtarget = null;
			
			done = true;
			if(sb1 != null){
				ballnode = node1;
				itemnode = node2;
				sbtarget = sb1;
			}else if(sb2 != null){
				ballnode = node2;
				itemnode = node1;
				sbtarget = sb2;
			}else{
				done = false;
			}
			if(done){
				cbtarget = sbtarget.getOwner();
				cbtarget.addMark(10);
				contact.setIgnored(true);
				destroyItem(itemnode.getName());
				System.out.println(cbtarget.getMark());
				return true;
			}
			
			return false;
		}
	}
	
//	public class ItemPickedAction implements InputActionInterface{
//
//		@Override
//		public void performAction(InputActionEvent evt) {
//			// TODO Auto-generated method stub
//			final ContactInfo info = (ContactInfo)evt;
//			OBJECT_TYPE node1type = (OBJECT_TYPE)info.getNode1().getUserData(OBJECT_TYPE.class.getName());
//			OBJECT_TYPE node2type = (OBJECT_TYPE)info.getNode2().getUserData(OBJECT_TYPE.class.getName());
//			if( node1type == OBJECT_TYPE.CONTROL_BALL )
//			{
//				ControlBall cb = (ControlBall)info.getNode1().getUserData(ControlBall.getControlBallTag());
//				info.getNode1().set
//				cb.addMark(10);
//			}
//		}
//	}
}
